// #ifdef H5
let jweixin = require('jweixin-module')
// #endif

/* 2022.10.20 兼容了抖音小程序 */

let pay = {}

/**
 * 支付
 * @param {object} orderInfo 支付参数
 * @param {int} pay_type 支付方式 1--微信支付 2--支付宝支付 3-抖音小程序
 * @date 2022-10-20
 */
pay.requestPayment = function(orderInfo = {}, pay_type = 1) {
	let arr = ['wxpay', 'alipay', 'toutiao']
	// 服务提供商
	let provider = arr[pay_type - 1]

	return new Promise((resolve, reject) => {
		// #ifdef APP-PLUS|MP-WEIXIN|MP-TOUTIAO|MP-ALIPAY
		// APP支付、微信小程序、抖音小程序、支付宝小程序
		uni.requestPayment({
			provider: provider,

			// #ifdef APP-PLUS|MP-TOUTIAO|MP-ALIPAY
			/* App端，微信支付 orderInfo 为 Object 类型，如下：
			{
				"appid": "wx5019a7e911f6f88e", 微信开放平台审核通过的应用APPID
				"partnerid": "1489696452", 微信支付分配的商户号
				"prepayid": "wx31113948217710665d3aef6324702a0000", 微信返回的支付交易会话ID
				"noncestr": "635f43844dc08", 随机字符串，不长于32位。
				"timestamp": 1667187588, 时间戳
				"package": "Sign=WXPay", 暂填写固定值Sign=WXPay
				"sign": "7A9F559B3C220416911931CB15C86B63" 签名
			}
			App端，支付宝支付 orderInfo 为 String 类型，如下：
			method=alipay.trade.app.pay
			&app_id=2021002128681519
			&timestamp=2022-11-02+13%3A22%3A23
			&format=json
			&version=1.0
			&alipay_sdk=alipay-easysdk-php-2.2.0
			&charset=UTF-8
			&sign_type=RSA2
			&app_cert_sn=168048eca0315ca9b2c00a2ca54e445c
			&alipay_root_cert_sn=687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6
			&biz_content=%7B%22subject%22%3A%22%E4%BD%99%E9%A2%9D%E5%85%85%E5%80%BC%E8%AE%A2%E5%8D%95%22%2C%22out_trade_no%22%3A%22M202211021322232422%22%2C%22total_amount%22%3A0.01%7D&notify_url=https%3A%2F%2Ftp.jucheng01.net%2Findex.php%2Fmoney%2Fapi%2FCallback%2FalipayNotify%2Fagent%2Fapp%2Fuid%2F2&sign=H9yN%2BnC5CPCbLIcXdMr0g6XTCtwmzi9P7Jf%2BNgl4WsRNhBQe%2FZJu3YduDF9aDU6Coyht0YJLUoZFZ%2FAnuJcwFuWSSsCvpESTKT0ar8W5DIAWFRpaD1KyIPi7OQwKkA%2F2w2K3LJZHwVcOrJSeIy6%2BRtsOfIfDmyexDioCtR%2Bufb9W0Eklw%2B40KW7lcN8aiWW%2BpK%2BdX6iJvW676VLCKqrHeuweuRDRFeHvndGDeIBhUYJCoEzjUEwYAlko1di6zJC9gd6ehKPB%2F7JUDykCdOOAwnhAKsWGvvUqmrmK6g%2FSze4O4O2UTTjESj%2BKFbdc4YvsqTNXZfZXfW5B%2BDGjNYVCfQ%3D%3D
			字节跳动小程序的 orderInfo 为 Object 类型，如下:
				order_id--担保交易服务端订单号
				order_token--担保交易订单号 token
			支付宝小程序的 orderInfo(支付宝的规范为 tradeNO) 为 String 类型，表示支付宝交易号 */
			orderInfo: orderInfo,
			// #endif

			// #ifdef MP-WEIXIN
			// 微信小程序 orderInfo.timeStamp时间戳S大写
			timeStamp: orderInfo.timeStamp,
			nonceStr: orderInfo.nonceStr,
			package: orderInfo.package,
			signType: orderInfo.signType,
			paySign: orderInfo.paySign,
			// #endif
			
			// #ifdef MP-TOUTIAO
			service: 5,
			// #endif
			
			success: (res) => {
				// console.log('requestPaymentSuccess')
				resolve(res)
			},
			fail: (err) => {
				// console.log('requestPaymentFail')
				reject(err)
			}
		})
		// #endif

		// #ifdef H5
		let user_agent = navigator.userAgent.toLowerCase()
		// 微信公众号的微信支付，需要单独调用JSSDK的支付
		if (user_agent.indexOf('micromessenger') > 0 && pay_type == 1) {
			return jweixin.wxPay({
				// 微信公众号的时间戳字段全小写
				timestamp: orderInfo.timestamp,
				nonceStr: orderInfo.nonceStr,
				package: orderInfo.package,
				signType: orderInfo.signType,
				paySign: orderInfo.paySign
			}).then(res => {
				console.log(res)
				resolve(res)
			}).catch(err => {
				console.log(err)
				console.log('fail:' + JSON.stringify(err))
				reject(err)
			})
		}
		// #endif
	})
}

// // 支付宝预授权
// uni.requestPayment({
// 	provider: provider,
// 	orderStr: orderInfo, //参数名和上面的不太一样
// 	success: (res) => {
// 		console.log(res)
// 		resolve(res)
// 	},
// 	fail: (err) => {
// 		console.log(err)
// 		console.log('fail:' + JSON.stringify(err))
// 		reject(err)
// 	}
// })


export default pay
